﻿using Infrastructure;
using Infrastructure.Tool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WsdShop.IService;
using WsdShop.IService.Common;
using WsdShop.Model;
using WsdShop.Model.SearchModels;
using WsdShop.Model.ViewModels;

namespace Admin.Controllers
{
    public class ProductController : BaseController
    {
        protected ISpecService specservice;
        protected IProductService productservice;
        protected ICarouselService carouselservice;
        protected IAdService adservice;
        protected ILogService logService;
        private IFreightService freightService;
        private ICommonAreaService areaService;
        public ProductController(IUserInfoService s, ISpecService _specservice, IProductService _productservice, ICarouselService _carouselservice, IAdService _adservice,ILogService l, IFreightService _freightService, ICommonAreaService _areaService) : base(s)
        {
            specservice = _specservice;
            productservice = _productservice;
            carouselservice = _carouselservice;
            adservice = _adservice;
            logService = l;
            freightService = _freightService;
            areaService = _areaService;
        }


        public JsonResult GetFreightList()
        {
            Result result = new Result();
            result.data = freightService.GetFreightTemplateList();
            result.isSuccess = true;
            return Json(result);

        }

        /// <summary>
        /// 规格类型管理
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "14")]
        [HttpGet]
        public ActionResult SpecTypeManage()
        {
            return View();
        }

        /// <summary>
        /// 规格类型分页数据
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "14")]
        [HttpPost]
        public ActionResult SpecTypeManage(BaseSearchModel search)
        {
            int total = 0;
            var list = specservice.SpecTypeManage(search, out total);
            return Json(new { Total = total, Results = list });
        }

        /// <summary>
        /// 新增、编辑数据保存
        /// </summary>
        /// <param name="specType"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "14")]
        [HttpPost]
        public ActionResult SpecTypeEdit(Product_SpecType specType)
        {
            return Json(specservice.SpecTypeEdit(specType, CurrentLoginUser.Id));
        }

        /// <summary>
        /// 删除规格类型
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "14")]
        [HttpPost]
        public ActionResult DelSpecType(int Id)
        {
            return Json(specservice.DelSpecType(Id, CurrentLoginUser.Id));
        }


        /// <summary>
        /// 规格模版管理
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "15")]
        [HttpGet]
        public ActionResult SpecTemplateManage()
        {
            return View();
        }

        /// <summary>
        /// 规格模版管理分页数据
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "15")]
        [HttpPost]
        public ActionResult SpecTemplateManage(SpecTemplateSearchModel search)
        {
            int total = 0;
            var list = specservice.SpecTemplateManage(search, out total);
            return Json(new { Total = total, Results = list });
        }

        /// <summary>
        /// 获取所有规格类型
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetSpecTypeList()
        {
            return Json(specservice.GetSpecTypeList());
        }

        /// <summary>
        /// 新增、编辑规格模版保存
        /// </summary>
        /// <param name="specTemplate"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "15")]
        [HttpPost]
        public ActionResult SpecTemplateEdit(Product_SpecTemplate specTemplate)
        {
            return Json(specservice.SpecTemplateEdit(specTemplate, CurrentLoginUser.Id));
        }

        /// <summary>
        /// 删除规格模版
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "15")]
        [HttpPost]
        public ActionResult DelSpecTemplate(int Id)
        {
            return Json(specservice.DelSpecTemplate(Id, CurrentLoginUser.Id));
        }

        /// <summary>
        /// 保存规格项
        /// </summary>
        /// <param name="TemplateId"></param>
        /// <param name="TempItems"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "15")]
        [HttpPost]
        public ActionResult SaveSpecTemplateItem(int TemplateId, string TempItems)
        {
            return Json(specservice.SaveSpecTemplateItem(TemplateId, TempItems, CurrentLoginUser.Id));
        }


        /// <summary>
        /// 产品管理
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "16")]
        [HttpGet]
        public ActionResult ProductManage()
        {
            return View();
        }

        /// <summary>
        /// 产品列表分页数据
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "16")]
        [HttpPost]
        public ActionResult ProductManage(ProductSearchModel search)
        {
            int total = 0;
            var list = productservice.ProductManage(search, out total);
            return Json(new { Total = total, Results = list });
        }

        /// <summary>
        /// 产品新增、编辑
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "16")]
        [HttpGet]
        public ActionResult ProductEdit()
        {
            return View();
        }

        /// <summary>
        /// 产品新增、编辑数据初始化
        /// </summary>
        /// <param name="Sku"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "16")]
        [HttpPost]
        public ActionResult ProductEdit(string Sku)
        {
            return Json(productservice.ProductEdit(Sku));
        }


        public JsonResult GetProductSpecType()
        {
            Result result = new Result();

            try
            {
                result.data = productservice.GetProductSpecType();
                result.isSuccess = true;
            }
            catch (Exception ex)
            {

                result.msg = ex.Message;
            }

            return Json(result);
        }

        /// <summary>
        /// 获取运费模版信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "16")]
        [HttpPost]
        public ActionResult GetFreightInfo(int Id)
        {
            return Json(productservice.GetFreightInfo(Id));
        }

        /// <summary>
        /// 变更规格类型
        /// </summary>
        /// <param name="Sku"></param>
        /// <param name="specTypeId"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "16")]
        [HttpPost]
        public ActionResult ChangeSpecType(string Sku, int specTypeId)
        {
            return Json(productservice.ChangeSpecType(Sku, specTypeId));
        }

        /// <summary>
        /// 选择规格项重置规格列表
        /// </summary>
        /// <param name="Sku"></param>
        /// <param name="chooseSpecItemList"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "16")]
        [HttpPost]
        public ActionResult ChooseSpecItem(string Sku, List<Product_SpecTemplateItem> chooseSpecItemList)
        {
            return Json(productservice.ChooseSpecItem(Sku, chooseSpecItemList));
        }

        /// <summary>
        /// 新增、编辑产品保存
        /// </summary>
        /// <param name="ProductMain"></param>
        /// <param name="SpecList"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "16")]
        [HttpPost]
        public ActionResult ProductEditSubmit(ProductInfo ProductMain, List<SpecResponse> SpecList)
        {
            var result = new Result();
            result = productservice.ProductEditSubmit(ProductMain, SpecList, CurrentLoginUser.Id, Request.UserHostAddress);
            if (result.isSuccess)
            {
                if (ProductMain.Id > 0)
                {
                    logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                            "编辑产品", $"编辑名称为{ProductMain.Name}的产品成功", Generator.GetIP());
                }
                else
                {
                    logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                            "新增产品", $"新增名称为{ProductMain.Name}的产品成功", Generator.GetIP());
                }

            }
            else
            {
                if (ProductMain.Id > 0)
                {
                    logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                            "编辑产品", $"编辑名称为{ProductMain.Name}的产品失败"+result.msg, Generator.GetIP());
                }
                else
                {
                    logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                            "新增产品", $"新增名称为{ProductMain.Name}的产品失败"+result.msg, Generator.GetIP());
                }
            }
            return Json(result);
        }

        /// <summary>
        /// 删除产品
        /// </summary>
        /// <param name="Sku"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "16")]
        [HttpPost]
        public ActionResult DelProduct(string Sku)
        {
            var result = new Result();
            result = productservice.DelProduct(Sku, CurrentLoginUser.Id, Request.UserHostAddress);
            if (result.isSuccess)
            {
                logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                            "删除产品", "操作成功", Generator.GetIP());
            }
            else
            {
                logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                            "删除产品", "操作失败", Generator.GetIP());
            }
            return Json(result);
        }

        /// <summary>
        /// 上下架
        /// </summary>
        /// <param name="Sku"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "16")]
        [HttpPost]
        public ActionResult ChangeDisplay(string Sku)
        {
            return Json(productservice.ChangeDisplay(Sku, CurrentLoginUser.Id, Request.UserHostAddress));
        }

        /// <summary>
        /// 评论管理
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "17")]
        [HttpGet]
        public ActionResult CommentManage()
        {
            return View();
        }

        /// <summary>
        /// 评论列表数据
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "17")]
        [HttpPost]
        public ActionResult CommentManage(ProductCommetSearchModel search)
        {
            int total = 0;
            var list = productservice.CommentManage(search, out total);
            return Json(new { Total = total, Results = list });
        }

        /// <summary>
        /// 回复评论
        /// </summary>
        /// <param name="comment"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "17")]
        [HttpPost]
        public ActionResult CommentReply(Product_Comment comment)
        {
            return Json(productservice.CommentReply(comment, CurrentLoginUser.Id));
        }

        /// <summary>
        /// 删除评论
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "17")]
        [HttpPost]
        public ActionResult DelComment(int Id)
        {
            return Json(productservice.DelComment(Id, CurrentLoginUser.Id));
        }

        /// <summary>
        /// 轮播图管理
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "18")]
        [HttpGet]
        public ActionResult CarouselManage()
        {
            return View();
        }

        /// <summary>
        /// 轮播图管理分页数据
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "18")]
        [HttpPost]
        public ActionResult CarouselManage(CarouselSearchModel search)
        {
            int total = 0;
            var list = carouselservice.CarouselManage(search, out total);
            return Json(new { Total = total, Results = list });
        }

        /// <summary>
        /// 新增、编辑轮播图
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "18")]
        [HttpGet]
        public ActionResult CarouselEdit()
        {
            return View();
        }

        /// <summary>
        ///  新增、编辑轮播图数据初始化
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "18")]
        [HttpPost]
        public ActionResult CarouselEdit(int Id)
        {
            return Json(carouselservice.CarouselEdit(Id));
        }

        /// <summary>
        /// 新增、编辑保存
        /// </summary>
        /// <param name="submitData"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "18")]
        [HttpPost]
        public ActionResult CarouselEditSubmit(Carousel submitData)
        {
            return Json(carouselservice.CarouselEditSubmit(submitData, CurrentLoginUser.Id));
        }

        /// <summary>
        /// 删除轮播图
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "18")]
        [HttpPost]
        public ActionResult DelCarousel(int Id)
        {
            return Json(carouselservice.DelCarousel(Id, CurrentLoginUser.Id));
        }

        /// <summary>
        /// 广告位管理
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "19")]
        [HttpGet]
        public ActionResult AdManage()
        {
            return View();
        }

        [AuthorizeFilter(MenuRole = "19")]
        [HttpPost]
        public ActionResult AdManage(AdSearchModel search)
        {
            int total = 0;
            var list = adservice.AdManage(search, out total);
            return Json(new { Total = total, Results = list });
        }

        /// <summary>
        /// 新增、编辑广告位
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "19")]
        [HttpGet]
        public ActionResult AdEdit()
        {
            return View();
        }

        /// <summary>
        ///  新增、编辑数据初始化
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "19")]
        [HttpPost]
        public ActionResult AdEdit(int Id)
        {
            return Json(adservice.AdEdit(Id));
        }

        /// <summary>
        /// 新增、编辑保存
        /// </summary>
        /// <param name="submitData"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "19")]
        [HttpPost]
        public ActionResult AdEditSubmit(AdConfiguration submitData)
        {
            return Json(adservice.AdEditSubmit(submitData, CurrentLoginUser.Id));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "19")]
        [HttpPost]
        public ActionResult DelAd(int Id)
        {
            return Json(adservice.DelAd(Id, CurrentLoginUser.Id));
        }


        public JsonResult AddSpecItem(Product_SpecTemplateItem model)
        {
            Result result = new Result();
            try
            {
                model.AddUserId = CurrentLoginUser.Id;
                model.AddTime = DateTime.Now;
                specservice.AddSpecItem(model);
                result.isSuccess = true;
            }
            catch (Exception ex)
            {

                result.msg = ex.Message;
            }
            return Json(result);
        }

        public JsonResult GetAreaTree(int pid)
        {
            return Json(areaService.GetAreaTreeDate(pid));
        }

        public ActionResult UpdateProductArea(List<string> AuthList, int ProductId)
        {
            var result = new Result();
            result = productservice.UpdateProductArea(AuthList, ProductId, CurrentLoginUser.Id);
           
            return Json(result);
        }
    }
}